import Taro, { Component } from '@tarojs/taro'
import { Swiper, SwiperItem, Image, View, Text } from '@tarojs/components'
import '../../assets/common.scss'
import CommonJs from '../../utils/common'
import '../news/newlist.scss'

export default class Index extends Component {
  constructor(props) {
    super(props);
    this.state = {
    	catlist:[],
    	catid:0,
    	catindex:0,
      result:[],
		  znwns:'',
		  pageNumber:0,
		  hasNextPage:false,
		  scrollPd:false,
		  pageheight:0,
      newsimg:[],
      shopId:Taro.getStorageSync('shopId'),
      webinfo:Taro.getStorageSync('webinfo')
    }
  }
  config = {
    navigationBarTitleText: '公告资讯'
  }
  componentWillMount () {
  	this.getnewcat()
    
  }
  getnewcat(){
  	//var url = '/advert/papi/v1.9/category?categoryTypeId.equals=4&deep.equals=2'
  	if(this.state.webinfo){
  		var url='/advert/papi/v1.9/category?categoryTypeId.in=1,4&deep.equals=1&shopId.equals='+this.state.shopId
  	}
  	else{
  		var url='/advert/papi/v1.9/category/firstCategory/2'
  	}
  	var _this = this
  	CommonJs.ajaxRequest(url, '', 'GET', function(ret) {
  		if(_this.state.webinfo){
  			var res=ret.data.list;
  		}
  		else{
  			var res=ret.data
  		}
  		_this.setState({
	      catlist:res,
	      catid:res[0].id
	    },function(){
	    	_this.getnewtitle(0);
	    })
  		
  	})
  }
  getnewtitle(pageNumber) {
    var _this = this
    var shopid = this.state.shopId
    var url = '/advert/papi/v1.9/news/newsCategory'
    
    if(this.state.catid){
    	url=url+'?categoryId.equals='+this.state.catid
    }
    var dates={
      size:10,
      page:pageNumber,
      sort:'createAt,DESC'
    }
      CommonJs.ajaxRequest(url, dates, 'GET', function(res) {
        if (res) {

          if(res.data.totalPages>pageNumber){
            var hasNextPage = true;
          }
          else{
            var hasNextPage = false;
          }
          var result = res.data.list
          var results = _this.state.result
          
          for(var i = 0; i < result.length; i++){
            results.push(result[i]);
          }
          if (hasNextPage) {
            _this.setState({
              result: results,
              znwns: '暂无数据',
              pageNumber: res.data.page,
              hasNextPage: hasNextPage,
              scrollPd: true
            })
          }
          else {
            _this.setState({
              result: results,
              znwns: '暂无数据',
              pageNumber: res.data.page,
              hasNextPage: hasNextPage,
              scrollPd: false
            })
          }
        } else {
          _this.setState({
            znwns: '暂无数据'
          })
        }
      },'wfw')
    }
    open(url,type){
     
      CommonJs.openTo(url);
    }
    loadMore(){
      var _this = this
      var pd = _this.state.scrollPd
      if(pd) {
        //防止多次触发滑动到底部事件
        _this.setState({
          scrollPd: false//禁止下拉
        })
        var pageNumber = _this.state.pageNumber + 1
        _this.getnewtitle(pageNumber)
      }else{
        var hasNextPage =  _this.state.hasNextPage
        if(hasNextPage){
          Taro.showToast({
            title:'正在加载数据...',
            icon: 'none',
            duration: 2000
          })
        }else{
          Taro.showToast({
            title:'没有更多数据啦！',
            icon: 'none',
            duration: 2000
          })
        }
      }
    }
  choose(id,index){
  	var _this=this;
  	this.setState({
	    catid:id,
	    catindex:index,
	    result:[]
	  },function(){
	  	_this.getnewtitle(0)
	  })
  }
  render () {
    const newsimg = this.state.result
    const catlist = this.state.catlist
    return (
      <View className='page'>
      	<View className='new-cat'>
	        <ScrollView className='news-cat-box' scrollX='true'>
	        	<Text style="display:none" className={this.state.catindex==-1?'new-cat-li new-cat-cur':'new-cat-li'} onClick={this.choose.bind(this,0,-1)}>全部</Text>
	        	{catlist&&catlist.map((item, index) => (
	        		<Text className={this.state.catindex==index?'new-cat-li new-cat-cur':'new-cat-li'} onClick={this.choose.bind(this,item.id,index)}>{item.categoryName}</Text>
	        	))}
	        </ScrollView>
        </View>
        <ScrollView className='qun-mingxibo' scrollY='true' onScrollToLower={this.loadMore.bind(this,'')}>
        <View className='new-lists'>
        	<View className='nodate' style={newsimg&&newsimg.length>0?'display:none':''}>暂无资讯内容</View>
        {newsimg.map((item, index) => (
        <View className='new-dl' onClick={this.open.bind(this,'/pages/news/newtitle?type='+item.id)}>
       			<View className='new-dt'>
       				<Image className='qun-userg' mode='widthFix' src={item.titlePic}></Image>	
       			</View>
       			<View className='new-dd'>
            <View className='new-tit'>{item.title}</View>
       				<View className='new-info'>
                  <Text className='newt'>{CommonJs.FormatDate(item.createAt,3)}</Text>
       				</View>
       			</View>
       		</View>
            ))}
        </View>
        </ScrollView>
      </View>
    )
  }
}